package com.taotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.EasyUIResult;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbItemCatMapper;
import com.taotao.mapper.TbItemParamMapper;
import com.taotao.pojo.*;
import com.taotao.service.TbItemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TbItemParamServiceImpl implements TbItemParamService {

    @Autowired
    private TbItemParamMapper tbItemParamMapper;

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    /**
     * 查询所有的规格模板
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public EasyUIResult getTbItemParamList(int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(new TbItemParamExample());

        List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(new TbItemCatExample());

        List<TbItemParamInheritance> tbItemParamInheritanceList = new ArrayList<>();
        for (TbItemParam tbItemParam : tbItemParamList){
            TbItemParamInheritance tbItemParamInheritance = new TbItemParamInheritance();
            tbItemParamInheritance.setId(tbItemParam.getId());
            tbItemParamInheritance.setItemCatId(tbItemParam.getItemCatId());
            tbItemParamInheritance.setParamData(tbItemParam.getParamData());
            tbItemParamInheritance.setCreated(tbItemParam.getCreated());
            tbItemParamInheritance.setUpdated(tbItemParam.getUpdated());
            for (TbItemCat tbItemCat : tbItemCatList){
                if (tbItemCat.getId().equals(tbItemParam.getItemCatId())){
                    tbItemParamInheritance.setItemCatName(tbItemCat.getName());
                }
            }
            tbItemParamInheritanceList.add(tbItemParamInheritance);
        }
        PageInfo<TbItemParamInheritance> pageInfo = new PageInfo<>(tbItemParamInheritanceList);
        EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(),tbItemParamInheritanceList);
        return easyUIResult;
    }

    /**
     * 查询商品类目对应的规格模板
     * @param id
     * @return
     */
    @Override
    public TaotaoResult getTbItemParamByItemCatId(long id) {
        TbItemParamExample condition = new TbItemParamExample();
        condition.createCriteria().andItemCatIdEqualTo(id);
        List<TbItemParam> tbItemParamList = tbItemParamMapper.selectByExampleWithBLOBs(condition);
        if(tbItemParamList.size() > 0) {
            return TaotaoResult.ok(tbItemParamList.get(0));
        }
        return TaotaoResult.build(400, "未查询到数据");
    }

    /**
     * 添加商品类目对应的规格模板
     * @param itemCatId
     * @param paramData
     * @return
     */
    @Override
    public TaotaoResult addTbItemParam(long itemCatId,String paramData) {
        TbItemParam tbItemParam = new TbItemParam();
        tbItemParam.setItemCatId(itemCatId);
        tbItemParam.setParamData(paramData);
        tbItemParam.setCreated(new Date());
        tbItemParam.setUpdated(new Date());

        tbItemParamMapper.insertSelective(tbItemParam);

        return TaotaoResult.build(200,"");
    }

    /**
     * 删除规格模板
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public TaotaoResult deleteTbItemParam(long[] ids) {
        int line = 0;
        for (int i = 0; i < ids.length; i++) {
            long id = ids[i];
            line = tbItemParamMapper.deleteByPrimaryKey(id) + line;
        }
        if (line == ids.length) {
            return TaotaoResult.build(200, "");
        }
        return null;
    }
}
